package org.aceor.mddal.group.util;

import com.google.common.collect.Maps;
import org.aceor.mddal.group.DataSourceWrapper;
import org.aceor.mddal.group.selector.EquityDBSelector;
import org.apache.commons.collections.CollectionUtils;

import java.util.Collection;
import java.util.Map;

/**
 * Created by lxue on 16/4/6.
 */
public class GroupDsUtil {

    public static EquityDBSelector createEquityDbManager(Collection<DataSourceWrapper> dsws, boolean isRead) {
        if (CollectionUtils.isEmpty(dsws)) {
            throw new NullPointerException("数据源集合不能为空");
        }
        Map<String, DataSourceWrapper> dataSourceMap = Maps.newHashMap();
        Map<String, Integer> weightMap = Maps.newHashMap();

        for (DataSourceWrapper dsw : dsws) {
            String dsKey = dsw.getDataSourceKey();
            dataSourceMap.put(dsKey, dsw);
            weightMap.put(dsKey, isRead ? dsw.getWeight().r : dsw.getWeight().w);
        }

        EquityDBSelector equityDBSelector = new EquityDBSelector(dataSourceMap, weightMap);
        equityDBSelector.setReadable(isRead);
        return equityDBSelector;
    }
}
